home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Applications / Newswatcher 2.0b22 / NW Source / Source / log.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-26  |  3.6 KB  |  157 lines  |  [TEXT/MMCC]

  1. /*----------------------------------------------------------------------------
  2.  
  3.     log.c
  4.  
  5.     This module handles logging.
  6.     
  7.     Copyright © 1994, Northwestern University.
  8.  
  9. ----------------------------------------------------------------------------*/
  10.  
  11. #include <Packages.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14.  
  15. #include "glob.h"
  16. #include "log.h"
  17. #include "dialog.h"
  18. #include "strutil.h"
  19. #include "fileutil.h"
  20. #include "resutil.h"
  21. #include "ic.h"
  22.  
  23.  
  24.  
  25. static short gRefNum = 0;
  26.  
  27.  
  28.  
  29. /*----------------------------------------------------------------------------
  30.     PutString 
  31.     
  32.     Write a string to the log file.
  33.     
  34.     Entry:    str = string to write.
  35. ----------------------------------------------------------------------------*/
  36.  
  37. static void PutString (char *str)
  38. {
  39.     char line[513];
  40.     long len;
  41.     
  42.     sprintf(line, "%s\r", str);
  43.     len = strlen(line);
  44.     MyFSWriteNoCache(gRefNum, &len, line, nil);
  45. }
  46.  
  47.  
  48.  
  49. /*----------------------------------------------------------------------------
  50.     OpenLogFile 
  51.     
  52.     Open the log file.
  53. ----------------------------------------------------------------------------*/
  54.  
  55. void OpenLogFile (void)
  56. {
  57.     FCBPBRec pBlock;
  58.     FSSpec logFile;
  59.     OSErr err = noErr;
  60.     Str255 versStr, fileName;
  61.     CStr255 str, fmt;
  62.     Boolean empty;
  63.     
  64.     MyICReadSharedPrefs(kICeditorHelper);
  65.     
  66.     if (gRefNum != 0) return;
  67.     pBlock.ioNamePtr = nil;
  68.     pBlock.ioVRefNum = 0;
  69.     pBlock.ioRefNum = LMGetCurApRefNum();
  70.     pBlock.ioFCBIndx = 0;
  71.     err = PBGetFCBInfo(&pBlock, false);
  72.     if (err != noErr) goto exit;
  73.     GetPString(kStrLogFileName, fileName);
  74.     err = FSMakeFSSpec(pBlock.ioFCBVRefNum, pBlock.ioFCBParID, 
  75.         fileName, &logFile);
  76.     if (err != noErr && err != fnfErr) goto exit;
  77.  
  78.     err = OpenDataForkWriteCreateIfMissing(&logFile, gPrefs.savedArtCreator, 'TEXT',
  79.         smSystemScript, false, &gRefNum, &empty);
  80.     if (err != noErr) goto exit;
  81.  
  82.     PutString("");
  83.     PutString("------------------------------------------------------------------------------");
  84.     PutString("");
  85.     GetCString(kStrLogOpenMsg, str);
  86.     PutString(str);
  87.     err = GetVersionString(versStr);
  88.     if (err != noErr) goto exit;
  89.     GetCString(kStrNewsWatcherVersion, fmt);
  90.     p2cstr(versStr);
  91.     sprintf(str, fmt, versStr);
  92.     PutString(str);
  93.     GetCString(kStrLogLegend, str);
  94.     PutString(str);
  95.     PutString("");
  96.     return;
  97.     
  98. exit:
  99.  
  100.     if (gRefNum != 0) MyFSClose(gRefNum, nil);
  101.     gRefNum = 0;
  102.     ErrorMessageNumber(kStrLogCantOpen);
  103. }
  104.  
  105.  
  106.  
  107. /*----------------------------------------------------------------------------
  108.     CloseLogFile 
  109.     
  110.     Close the log file.
  111. ----------------------------------------------------------------------------*/
  112.  
  113. void CloseLogFile (void)
  114. {
  115.     CStr255 str;
  116.  
  117.     if (gRefNum == 0) return;
  118.     PutString("");
  119.     GetCString(kStrLogClosed, str);
  120.     PutString(str);
  121.     MyFSClose(gRefNum, nil);
  122.     gRefNum = 0;
  123. }
  124.  
  125.  
  126.  
  127. /*----------------------------------------------------------------------------
  128.     Log 
  129.     
  130.     Log a server command or response.
  131.     
  132.     Entry:    command = true if command, false if response.
  133.             ipAddr = IP address of server.
  134.             str = command or response.
  135. ----------------------------------------------------------------------------*/
  136.  
  137. void Log (Boolean command, unsigned long ipAddr, char *str)
  138. {
  139.     char line[512];
  140.     char c;
  141.     short o1, o2, o3, o4;
  142.  
  143.     if (gRefNum == 0) return;
  144.     c = command ? 'C' : 'R';
  145.     o1 = (ipAddr >> 24) & 0xff;
  146.     o2 = (ipAddr >> 16) & 0xff;
  147.     o3 = (ipAddr >> 8) & 0xff;
  148.     o4 = ipAddr & 0xff;
  149.     if (command && MyStrNEqual(str, "PASS", 4)) {
  150.         sprintf(line, "C %d.%d.%d.%d PASS *******", o1, o2, o3, o4);
  151.     } else if (command && MyStrNEqual(str, "AUTHINFO PASS", 13)) {
  152.         sprintf(line, "C %d.%d.%d.%d AUTHINFO PASS *******", o1, o2, o3, o4);
  153.     } else {
  154.         sprintf(line, "%c %d.%d.%d.%d %s", c, o1, o2, o3, o4, str);
  155.     }
  156.     PutString(line);
  157. }